﻿using DotNetClient;
using DoudizhuMsg;
using Frameworks;
using RoomMsg;
using UnityEngine;

namespace Doudizhu
{
    public class DoudizhuGame : MonoBehaviour
    {
        public GameObject player_template;
        public GameObject playerOne;
        public GameObject playerTwo;
        public GameObject playerThree;

        private void Start()
        {
            EventProxy.AddEventListener(Routes.S2C_DOUDIZHU_ON_GAME_INIT, onPushDoudizhuGameInit);
        }

        private void OnDestroy()
        {
            EventProxy.RemoveEventListener(Routes.S2C_DOUDIZHU_ON_GAME_INIT, onPushDoudizhuGameInit);
        }

        private void onPushDoudizhuGameInit(EventData eventData)
        {
            s2c_onGameInit data = eventData.target as s2c_onGameInit;

            // TODO: 发牌动画

            // 生成玩家实体
            for (int i = 0; i < data.Players.Count; i++)
            {
                GamePlayer gamePlayer = data.Players[i];
                Player player = DoudizhuData.Instance.GetPlayerByUid(gamePlayer.Uid).player;

                generateGamePlayer(player, gamePlayer);
            }
            updateSeat();

            c2s_playerAction msg = new c2s_playerAction() { Action = (uint)PLAYER_ACTION.START };
            NetworkManager.Instance.Notify(Routes.C2S_ROOM_PLAYER_ACTION, msg);
        }

        private void generateGamePlayer(Player player,GamePlayer gamePlayer)
        {
            GameObject playerGo = Instantiate(player_template);
            playerGo.SetActive(true);

            GamePlayerEntity playerEntity = new GamePlayerEntity(playerGo);
            playerEntity.Update(player, gamePlayer);

            DoudizhuData.Instance.AddGamePlayer(playerEntity);

            if(gamePlayer.Uid == DoudizhuData.Instance.my.Uid)
            {
                DoudizhuData.Instance.myGame = gamePlayer;
            }
        }

        private void updateSeat()
        {
            for (int i = 0; i < DoudizhuData.Instance.gamePlayers.Count; i++)
            {
                GamePlayerEntity playerEntity = DoudizhuData.Instance.gamePlayers[i];
                playerEntity.UpdateSeat(getSeat(playerEntity.gamePlayer.Seat));
            }
        }

        private Transform getSeat(int seat)
        {
            int mySeat = DoudizhuData.Instance.myGame.Seat;

            if (mySeat == 0)
            {
                if (seat == 0) return playerOne.transform;
                else if (seat == 1) return playerTwo.transform;
                else if (seat == 2) return playerThree.transform;
            }
            else if (mySeat == 1)
            {
                if (seat  == 0) return playerThree.transform;
                else if (seat  == 1) return playerOne.transform;
                else if (seat == 2) return playerTwo.transform;
            }
            else if (mySeat == 2)
            {
                if (seat == 0) return playerTwo.transform;
                else if (seat == 1) return playerThree.transform;
                else if (seat == 2) return playerOne.transform;
            }

            return null;
        }
    }
}
